package de.hdodenhof.circleimageview.sample.ui.fragment;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;

import androidx.fragment.app.Fragment;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnItemSelected;
import de.hdodenhof.circleimageview.sample.R;
import de.hdodenhof.circleimageview.sample.ui.fragment.eventbus.PicWrap;
import es.dmoral.toasty.Toasty;

public class ToastyFragment extends Fragment {
    @BindView(R.id.et_message)
    EditText mEtMessage;

    @BindView(R.id.spinner_show_type)
    Spinner mSpinnerShowType;

    @BindView(R.id.cb_with_icon)
    CheckBox mCbWithIcon;

    @BindView(R.id.cb_should_tint)
    CheckBox mCbShouldTint;



    public static ToastyFragment newInstance() {
        ToastyFragment fragment = new ToastyFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_toasty, null);

        ButterKnife.bind(this, view);


        mSpinnerShowType.setAdapter(new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1, new String[]{
                "错误提示",
                "正确提示",
                "消息提示",
                "自定义提示"
        }));
        mSpinnerShowType.setSelection(0);
        return view;
    }

    @OnItemSelected(R.id.spinner_show_type)
    public void changeType(View view){
        mCbWithIcon.setVisibility(mSpinnerShowType.getSelectedItemPosition() != 3 ? View.GONE : View.VISIBLE);
    }

    @OnClick(R.id.btn_show_toast)
    public void showToast(){
        String message = mEtMessage.getText().toString();
        boolean isWithIcon = mCbWithIcon.isChecked();
        boolean isShouldTint = mCbShouldTint.isChecked();
        Context context = getContext();

        int type = mSpinnerShowType.getSelectedItemPosition();

        switch (type) {
            case 0:
                Toasty.error(context, message, Toast.LENGTH_LONG, isWithIcon).show();
                break;
            case 1:
                Toasty.success(context, message, Toast.LENGTH_LONG, isWithIcon).show();
                break;
            case 2:
                Toasty.info(context, message, Toast.LENGTH_LONG, isWithIcon).show();
                break;
            case 3:
                Toasty.Config config = Toasty.Config.getInstance();
                Toasty.custom(context, message, getResources().getDrawable(R.drawable.ic_launcher, null),
                        Color.RED,
                        Color.GREEN,
                        Toasty.LENGTH_SHORT,
                        isWithIcon,
                        isShouldTint).show();
                break;
        }

    }


    @OnClick(R.id.btn_pic_change)
    public void changePic(){
        String url = "https://image.baidu.com/search/down?tn=download&word=download&ie=utf8&fr=detail&url=https%3A%2F%2Ftimgsa.baidu.com%2Ftimg%3Fimage%26quality%3D80%26size%3Db9999_10000%26sec%3D1582565111119%26di%3Dcc8e32498ded24251355cebad324aefd%26imgtype%3D0%26src%3Dhttp%253A%252F%252Fyouimg1.c-ctrip.com%252Ftarget%252Ftg%252F096%252F755%252F666%252F49611e232c4646bcbfdca563a39b15ab.jpg&thumburl=https%3A%2F%2Fss2.bdstatic.com%2F70cFvnSh_Q1YnxGkpoWK1HF6hhy%2Fit%2Fu%3D2631377660%2C2065981666%26fm%3D26%26gp%3D0.jpg";
        EventBus.getDefault().post(new PicWrap(url));
        Toasty.info(getContext(), "Glide页面的图片改变了", Toast.LENGTH_LONG).show();

    }
    @OnClick(R.id.btn_pic_change_sticky)
    public void changePicSticky(){
        String url = "https://image.baidu.com/search/down?tn=download&word=download&ie=utf8&fr=detail&url=https%3A%2F%2Ftimgsa.baidu.com%2Ftimg%3Fimage%26quality%3D80%26size%3Db9999_10000%26sec%3D1582567915165%26di%3D8e8db4a95ab35cf5fab96c56221532cf%26imgtype%3D0%26src%3Dhttp%253A%252F%252Fphotocdn.sohu.com%252F20160223%252Fmp60176446_1456227337709_17.jpeg&thumburl=https%3A%2F%2Fss0.bdstatic.com%2F70cFuHSh_Q1YnxGkpoWK1HF6hhy%2Fit%2Fu%3D2253592489%2C3119957287%26fm%3D26%26gp%3D0.jpg";
        EventBus.getDefault().postSticky(new PicWrap(url));
        Toasty.info(getContext(), "Glide页面的图片改变了", Toast.LENGTH_LONG).show();

    }


}
